<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <input type="text" id="txt">
  <div id="show"></div>
  <input type="text" id="phone" placeholder="输入手机号">
  <button id="btn">替换</button>
  <script>
    // 1 str.replace("要替换的字符","替换内容")
    // str.replace("要替换的字符"的正则,"替换内容")
    var reg = /(TMD|MD|cao|艹)/g
    var btn = document.getElementById("btn");
    btn.onclick = function () {
      //  取出输入框的值
      var txtVal = document.getElementById("txt").value;
      var newstr = txtVal.replace(reg, "***");
      var show = document.getElementById("show");
      var phoneVal = document.getElementById("phone").value;
      var phonestr = phoneVal.replace(phoneVal.slice(3, 7), "****")
      show.innerHTML = newstr + "-----" + phonestr;
    }



  </script>
</body>

</html>